package com.smmisia.auth.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.smmisia.auth.aop.Encrypted;
import com.smmisia.common.request.auth.LoginRequest;
import com.smmisia.common.request.auth.CodeRequest;
import com.smmisia.common.request.auth.RegisterRequest;
import com.smmisia.auth.service.UserService;
import com.smmisia.common.Result.Result;
import com.smmisia.common.entity.auth.ApiResponse;
import com.smmisia.common.entity.auth.User;
import com.smmisia.common.response.auth.CodeResponse;
import com.smmisia.common.response.auth.UserResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.Executors;

/**
 * @author: smmisia
 * @date: 2023-09-08
 * @description: 用户管理
 */
@RestController
@RequestMapping("/user")
@Tag(name = "登录管理")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;


    private final String PREFIX = "ip:";

    /**
     * 获取验证码
     *
     * @param email
     * @return
     */
    @PostMapping("/code")
    @Operation(summary = "获取验证码", description = "获取验证码")
    public Result<CodeResponse> code(@Valid @RequestBody CodeRequest email) {
        return userService.code(email);
    }

    /**
     * 用户注册
     *
     * @param request
     * @return
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册")
    public Result<String> register(@Valid @RequestBody RegisterRequest request) {
        return userService.register(request);
    }

    /**
     * 用户登录
     *
     * @param loginRequest
     * @return
     */
    @PostMapping("/doLogin")
    @Operation(summary = "用户登录", description = "用户登录")
    public Result<String> doLogin(@Valid @RequestBody LoginRequest loginRequest) {
        return userService.doLogin(loginRequest);
    }

    @GetMapping("/callback")
    @Operation(summary = "回调")
    public Result<String> callback() {
        return Result.ok("回调成功");
    }

    @GetMapping(value = "/info")
    @Operation(summary = "获取用户信息")
    public User info(@RequestParam("id") Integer id) {
        User user = userService.getUserById(id);
        return user;
    }

    @GetMapping(value = "/getById")
    @Operation(summary = "获取用户信息")
    public Result<UserResponse> getById(@RequestParam("id") Integer id) {
        User user = userService.getUserById(id);
        UserResponse userResponse = UserConvertor.INSTANCE.mapToVo(user);
        log.info(JSON.toJSONString(user));
        return Result.ok(userResponse);
    }

    @GetMapping("/test")
    @Operation(summary = "解密")
    @Encrypted
    public Result<String> test(String body) {
        return Result.ok(body);
    }

    @Encrypted
    @GetMapping("/encrypt")
    @Operation(summary = "加密")
    public Result<String> encryptExample() {
        String body = "这是加密的body";
        return Result.ok(body);
    }

    @GetMapping("/ipAddress")
    @Operation(summary = "获取ip地址")
    public Result<String> ipAddress(String ip) {
        String loginId = (String) StpUtil.getLoginId();
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", "10001331");
        paramMap.put("key", "04bc4ccb964606d2487b09a1959d2b97");
        paramMap.put("ip", ip);

        String response =HttpUtil.get("http://49.234.56.78/api/ip/chaapi.php", paramMap);
        //将json字符串转换为对象
        ApiResponse apiResponse = JSON.parseObject(response, ApiResponse.class);
        // 拼接四川·绵阳
        String result = apiResponse.getSheng() + "·" + apiResponse.getShi();
        redisTemplate.opsForValue().set(PREFIX+loginId,result);
        return Result.ok(result);
    }

    @GetMapping("/jx")
    @Operation(summary = "抖音解析")
    public Result<String> dyjx(@RequestParam(value = "url") String url) throws IOException {
        return userService.jx(url);
    }
}